package com.telecom.restaurant.controllor.manage;

import com.telecom.restaurant.constant.DataResultConstant;
import com.telecom.restaurant.constant.ManageThreadLocal;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.domin.PaginationResult;
import com.telecom.restaurant.domin.vo.OrderVO;
import com.telecom.restaurant.enums.OrderStatusEnum;
import com.telecom.restaurant.model.param.OrderSearchParam;
import com.telecom.restaurant.service.OrderService;
import com.telecom.restaurant.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * Created by qiqi on 2019/11/23.
 */
@Controller
@RequestMapping("/restaurant/order/")
public class RestaurantOrderController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderService orderService;

    @RequestMapping("/searchAndPick")
    @ResponseBody
    public DataResult searchAndPick(String mobile, String pickCode) {

        if (StringUtils.isBlank(mobile) && StringUtils.isBlank(pickCode)) {
            return DataResultConstant.WRONG_PARAM;
        }
        if (StringUtils.isBlank(mobile)) {
            mobile = null;
        }
        if (StringUtils.isBlank(pickCode)) {
            pickCode = null;
        }

        // 查询当天的订单
        OrderSearchParam param = new OrderSearchParam();

        Long restaurantId = ManageThreadLocal.restaurantId.get();

        param.setPickDate(new Date());
        param.setRestaurantId(restaurantId);
        param.setCustomerMobile(mobile);
        param.setPickCode(pickCode);
        param.setPagination(false);
        param.setStatus(OrderStatusEnum.WAIT_PICK.getCode());

        List<OrderVO> orderVOList = orderService.pickOrder(param);

        DataResult dataResult = DataResultConstant.buildSuccessResult(orderVOList);
        return dataResult;
    }

    @RequestMapping("/pickOrder")
    @ResponseBody
    public DataResult pickOrder(Long orderId) {

        if (orderId == null) {
            return DataResultConstant.WRONG_PARAM;
        }

        // 通过订单Id取餐
        OrderSearchParam param = new OrderSearchParam();
        Long restaurantId = ManageThreadLocal.restaurantId.get();
        param.setPickDate(new Date());
        param.setRestaurantId(restaurantId);
        param.setPagination(false);
        param.setOrderId(orderId);
        param.setStatus(OrderStatusEnum.WAIT_PICK.getCode());

        List<OrderVO> orderVOList = orderService.pickOrder(param);

        DataResult dataResult = DataResultConstant.buildSuccessResult(orderVOList);
        return dataResult;
    }


    @RequestMapping("/searchOrder")
    @ResponseBody
    public DataResult searchOrder(String mobile, String date, Integer orderStatus, Integer pageSize, Integer pageNo) {

        // 查询当天的订单
        OrderSearchParam param = new OrderSearchParam();
        if (StringUtils.isNotBlank(mobile)) {
            param.setCustomerMobile(mobile);
        }

        if (StringUtils.isNotBlank(date)) {
            Date orderDate = DateUtil.convertString2Date(date);
            param.setOrderDate(orderDate);
        }
        if (orderStatus != null) {
            param.setStatus(orderStatus);
        }
        param.setPageNo(pageNo);
        param.setPageSize(pageSize);
        param.setUseDesc(true);

        Long restaurantId = ManageThreadLocal.restaurantId.get();
        param.setRestaurantId(restaurantId);

        PaginationResult result = orderService.searchOrderForShow(param);
        DataResult dataResult = DataResultConstant.buildSuccessResult(result);
        return dataResult;
    }


    @RequestMapping("/exportOrderForPickFood")
    public void exportOrderForPickFood(HttpServletResponse response, String date) {

        Long restaurantId = ManageThreadLocal.restaurantId.get();
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        try {
            Date orderDate = new Date();
            if (StringUtils.isNotBlank(date)) {
                DateUtil.convertString2Date(date);
            }
            String fileName = DateUtil.convertDate2DateString(orderDate) + "_取餐列表";
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
            orderService.exportTodayOrderList(restaurantId, orderDate, response.getOutputStream());
        } catch (Exception e) {
            logger.error("下载失败", e);
        }
    }


}
